$(function () {
    var baseUrl=window.location.protocol+"//"+window.location.host;

    //文章标题切换
    $('.article-list .article-title div').on('click',function () {

        $(this).siblings().removeClass('active');
        $(this).addClass('active');
        var category_id=$(this).attr('data-category_id');
        var content=$(this).parent().parent().find('.article-content');
        $.ajax({
            type: "POST",
            url: baseUrl+"/api/article/getIndexArticleList",
            data: {category_id:category_id,num:15},
            dataType: "json",
            success: function(data){
                if(data.code==1){
                    var articleList=data.data.articleList;
                    var html='';
                    html+='<div class="content-item">';
                    html+='<div class="item-content">';
                    $.each(articleList,function (i,v) {
                       html+='<div><a href="'+v.link+'" target="_blank">'+v.title+'</a></div>';
                    });
                    html+='</div>';
                    html+='</div>';
                    $(content).html('').html(html);
                }
            }
        });
    });

    //文章图片标题切换
    $('.article-thumb .article-title div').on('click',function () {
        $(this).siblings().removeClass('active');
        $(this).addClass('active');

        var category_id=$(this).attr('data-category_id');
        var content=$(this).parent().parent().find('.article-content');

        $.ajax({
            type: "POST",
            url: baseUrl+"/api/article/getIndexArticleList",
            data: {category_id:category_id,num:4},
            dataType: "json",
            success: function(data){
                if(data.code==1){
                    var category=data.data.category;
                    var articleList=data.data.articleList;
                    var html='';
                    $.each(articleList,function (i,v) {
                        html+='<div class="content-item">';
                        html+='<div class="item-title" onclick="window.open(\''+v.link+'\')">';
                        if(v.isding) {
                            html+='<span class="ding">【顶】</span>';
                        }
                        html+=v.title + '</div>';
                        html+='<div class="item-content">';
                        html+='<div class="left-box">';
                        html+='<img src="'+v.thumb+'" alt="'+v.title+'" title="'+v.title+'" onclick="window.open(\''+v.link+'\')">';
                        html+='<span>'+category.name+'</span>';
                        html+='</div>';
                        html+='<div class="right-box">';
                        html+='<div class="content">'+v.info+'</div>';
                        html+='<div class="info">';
                        html+='<div class="user">';
                        html+='<div class="avatar"><img src="'+v.avatar+'"></div>';
                        html+='<span class="publish-date">'+v.createtime+'</span>';
                        html+='<span class="category">['+category.name+']</span>';
                        html+='</div>';
                        html+='<div class="read-more">';
                        html+='<a href="'+v.link+'" target="_blank">阅读更多</a>';
                        html+='</div>';
                        html+='</div>';
                        html+='</div>';
                        html+='</div>';
                        html+='</div>';
                    });
                    $(content).html('').html(html);
                }
            }
        });
    });

    // 点击弹出微信二维码
    $('.popup-wxqrcode-box').on('click',function () {
        if($(this).attr('status')==1){
            $(this).find('.popup-wxqrcode').hide();
            $(this).removeAttr('status');
        }else{
            $(this).find('.popup-wxqrcode').show();
            $(this).attr('status',1);
        }
    });

    var s = Snap(document.getElementById("clock"));

    var seconds = s.select("#seconds"),
        minutes = s.select("#minutes"),
        hours   = s.select("#hours"),
        rim     = s.select("#rim"),
        face    = {
            elem: s.select("#face"),
            cx: s.select("#face").getBBox().cx,
            cy: s.select("#face").getBBox().cy,
        },
        angle   = 0,
        easing = function(a) {
            return a==!!a?a:Math.pow(4,-10*a)*Math.sin((a-.075)*2*Math.PI/.3)+1;
        };

    function update() {
        var time = new Date();
        setHours(time);
        setMinutes(time);
        setSeconds(time);
    }

    function setHours(t) {
        var hour = t.getHours();
        hour %= 12;
        hour += Math.floor(t.getMinutes()/10)/6;
        var angle = hour*360/12;
        hours.animate(
            {transform: "rotate("+angle+" 244 251)"},
            100,
            mina.linear
        );
    }
    function setMinutes(t) {
        var minute = t.getMinutes();
        minute %= 60;
        minute += Math.floor(t.getSeconds()/10)/6;
        var angle = minute*360/60;
        minutes.animate(
            {transform: "rotate("+angle+" "+face.cx+" "+face.cy+")"},
            100,
            mina.linear
        );
    }
    function setSeconds(t) {
        t = t.getSeconds();
        t %= 60;
        var angle = t*360/60;
        //if ticking over to 0 seconds, animate angle to 360 and then switch angle to 0
        if (angle === 0) angle = 360;
        seconds.animate(
            {transform: "rotate("+angle+" "+face.cx+" "+face.cy+")"},
            600,
            easing,
            function(){
                if (angle === 360){
                    seconds.attr({
                        transform: "rotate("+0+" "+face.cx+" "+face.cy+")",
                    })
                }
            }
        );
    }
    setInterval(update, 1000);

});